package com.example.gaoyang.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.gaoyang.domain.Way;
import com.example.gaoyang.domain.request.WayRequest;
import com.example.gaoyang.domain.vo.WayVO;
import com.example.gaoyang.mapper.WayMapper;
import com.example.gaoyang.service.WayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@Transactional
public class WayServiceImpl extends ServiceImpl<WayMapper, Way> implements WayService {


    @Override
    public void addWay(WayRequest wayRequest) {
        Way way = BeanUtil.copyProperties(wayRequest, Way.class);
        this.save(way);
    }

    @Override
    public List<WayVO> getWayList(WayRequest wayRequest) {
        LambdaQueryWrapper<Way> wrapper = this.getQueryWrapper(wayRequest);
        List<WayVO> list = BeanUtil.copyToList(this.list(wrapper), WayVO.class);
        return list;
    }

    @Override
    public void updateWay(WayRequest wayRequest) {
        Way way = BeanUtil.copyProperties(wayRequest, Way.class);
        this.updateById(way);
    }

    @Override
    public void deleteWay(String id) {
        this.removeById(id);
    }

    private LambdaQueryWrapper<Way> getQueryWrapper(WayRequest wayRequest) {
        return new LambdaQueryWrapper<Way>()
                .eq(StringUtils.isNotEmpty(wayRequest.getId()), Way::getId, wayRequest.getId())
                .like(StringUtils.isNotEmpty(wayRequest.getDescription()), Way::getDescription, wayRequest.getDescription())
                .eq(wayRequest.getLevel() != null, Way::getLevel, wayRequest.getLevel())
                .eq(StringUtils.isNotEmpty(wayRequest.getRoleId()), Way::getRoleId, wayRequest.getRoleId())
                .like(StringUtils.isNotEmpty(wayRequest.getWayHighSpeedName()), Way::getWayHighSpeedName, wayRequest.getWayHighSpeedName());
    }
}
